package com.aa;
import org.json.JSONException;
import org.json.JSONObject;

import com.aa.http.HttpOperation;


import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ChongZhiActivity extends Activity {
	private final String mUrl = "http://121.9.64.196:8087/AAproject/api/mobile.php";
	private TextView mtvPhoneNumber;
	private EditText metCardNumber ;
	private EditText metCardPassword;
	private EditText metChongZhiAcount;
	private TextView mTitileView;
	private TextView mtvChongZhiTip;
	private Button mbtnBack;
	private Button mbtnChongzhi;
	private String mCardName;
	private int miType = 10;
	private String mCardIntro;
	private ProgressDialog mProgressDialog = null;
	private Handler mHandler;
	private String mErrorStr;
	private int type = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chongzhi);
		initVarible();
		initView();
		initListener();
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
			
				super.handleMessage(msg);
				
				switch (msg.what) {
				case 0:
					Toast.makeText(ChongZhiActivity.this, R.string.app_chongzhi_success, 3000).show();
					finish();
					break;
				case 1:
					Toast.makeText(ChongZhiActivity.this, R.string.app_chongzhi_fail, 3000).show();
					break;
				case 2:
					Toast.makeText(ChongZhiActivity.this, R.string.http_connect_outtime, 3000).show();
					break;
				case 3:
					Toast.makeText(ChongZhiActivity.this, mErrorStr, 3000).show();
					break;
				}
			}
			
		};
	}

	private void initVarible(){
		Intent _Intent = getIntent();
		Bundle _Bundle = _Intent.getBundleExtra("KEY");
		mCardName = _Bundle.getString("NAME");
		miType = _Bundle.getInt("TYPE");
		
		if (miType == 0) {
			mCardIntro = getString(R.string.chongzhi_intro1);
			type = 4;
		}else if(miType == 1){
			mCardIntro = getString(R.string.chongzhi_intro2);
			type = 2;
		}else if (miType == 3) {
			mCardIntro = getString(R.string.chongzhi_intro3);
			type = 1;
		}else if (miType == 2) {
			mCardIntro = getString(R.string.chongzhi_intro4);
			type = 3;
		}
	}
	private void initView(){
		
		mtvPhoneNumber = (TextView)findViewById(R.id.czs1_tip1);
		mtvPhoneNumber.setText(getString(R.string.app_telephone_tip) + Common.iMyPhoneNumber);
		
		metCardNumber = (EditText)findViewById(R.id.czs1_edit1);
		metCardPassword = (EditText)findViewById(R.id.czs1_edit2);
		metChongZhiAcount = (EditText)findViewById(R.id.czs1_edit3);
		
		mTitileView = (TextView)findViewById(R.id.app_title_center);
		mTitileView.setText(mCardName);
		
		mbtnBack = (Button)findViewById(R.id.title_btn1);
		mbtnBack.setVisibility(View.FIND_VIEWS_WITH_TEXT);
		mbtnBack.setText(R.string.app_back_tip);
		
		mbtnChongzhi = (Button)findViewById(R.id.title_btn4);
		mbtnChongzhi.setVisibility(View.FIND_VIEWS_WITH_TEXT);
		mbtnChongzhi.setText(R.string.app_cz_tip);
		
		mtvChongZhiTip = (TextView)findViewById(R.id.czs1_tip2);
		mtvChongZhiTip.setText(mCardIntro);
	}
	
	private void initListener(){
		mbtnBack.setOnClickListener(new OnChongzhiClicklistener());
		mbtnChongzhi.setOnClickListener(new OnChongzhiClicklistener());
		
	}
	
	private class OnChongzhiClicklistener implements OnClickListener{

		@Override
		public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_btn1:
			finish();
			break;
		case R.id.title_btn4:
			chongZhi();
		default:
			break;
		}
			
		}
		
	}
	
	private void chongZhi(){
	    if ((Common.iBalance == null) || (Common.iBalance.length() == 0) || (Common.iMyPhoneNumber == null) || (Common.iMyPhoneNumber.length() == 0))
	    {
	     
	      Toast.makeText(ChongZhiActivity.this, getString(R.string.app_tip_bangding), 3000).show();
	      return;
	    }
	    String _CardNumber = metCardNumber.getText().toString();
	    if (_CardNumber.length() == 0)
	    {
	     
	      Toast.makeText(ChongZhiActivity.this, getString(R.string.chongzhi_4), 3000).show();
	      return;
	    }
	    String _Password = metCardPassword.getText().toString();
	    if (_Password.length() == 0)
	    {
	      Toast.makeText(getApplicationContext(), getString(R.string.chongzhi_5), 3000).show();
	      return;
	    }
	    String _Acount = metChongZhiAcount.getText().toString();
	    if (_Acount.length() == 0)
	    {
	     
	      Toast.makeText(ChongZhiActivity.this, getString(R.string.chongzhi_6), 3000).show();
	      return;
	    }
	    if (((miType == 1) || (miType == 0)) && (!_Acount.equals("30")) && (!_Acount.equals("50")) && (!_Acount.equals("100")))
	    {
	     
	      Toast.makeText(ChongZhiActivity.this, getString(R.string.app_chongzhi_351), 3000).show();
	      return;
	    }
	    submitChongzhi(_CardNumber, _Password, _Acount);
	}
	
	private void submitChongzhi(String pCardNumber, String pCardPassword,String pAcount){
		
		mProgressDialog = ProgressDialog.show(this, getString(R.string.app_progress_title), 
				getString(R.string.app_progress_tip), true, false);
		mProgressDialog.setCancelable(true);
		
		final JSONObject _SendJson = new JSONObject();
		JSONObject _BodyJson = new JSONObject();
		JSONObject _HeadJson = new JSONObject();
		try {
			_HeadJson.put("v", Common.iVersion);
			_HeadJson.put("time", System.currentTimeMillis());
			_HeadJson.put("cmd", 8);
			_HeadJson.put("mac", Common.iMac);
			
			_BodyJson.put("uid", Common.iUID);
			_BodyJson.put("pnumber", Common.iMyPhoneNumber);
			_BodyJson.put("cardnum", pCardNumber);
			_BodyJson.put("cardpasswd", pCardPassword);
			_BodyJson.put("type", type);
			_BodyJson.put("balance", pAcount);
			
			_SendJson.put("head", _HeadJson);
			_SendJson.put("body", _BodyJson); 
			Log.i("chongzhi", _SendJson.toString());
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		
		new Thread(new Runnable() {				
			@Override
			public void run() {
				String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
				mProgressDialog.dismiss();
				
				if (_Result == null) {
					
					mHandler.sendEmptyMessage(1);
					return;
				}
				Log.i("chongzhi", "returnResult = " + _Result);
				if (_Result.equals("ConnectTimeout")) {
					mHandler.sendEmptyMessage(2);
					return;
				}
				try {
					  JSONObject _JsonObject = new JSONObject(_Result);
					  JSONObject _Body = _JsonObject.getJSONObject("body");
					  if (_Body.getInt("status") == 1) {
						  Common.iBalance = _Body.getString("balance");
						 
						  mHandler.sendEmptyMessage(0);
						
						  return;
						
					}else {
						mErrorStr = _Body.getString("msg");
						mHandler.sendEmptyMessage(3);
						return;
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
					
				}
				
			}
		}).start();
	}
}
